#pragma once

#include <string>
#include <atomic>
#include "event.h"


namespace evm { namespace event {

using namespace std;

constexpr int MAX_CONNECTIONS = 65536;

using TCPHandler = function<void (int fd, EventLoop* loop)>;

class TCPServer {
public:
    TCPServer(const string &host, uint16_t port,
              EventLoop* loop = nullptr, TCPHandler handler = TCPHandler());

    virtual ~TCPServer();

    virtual void start();
    virtual void stop();

    EventLoop* get_loop() {
        return loop_;
    }

    void set_loop(EventLoop* loop) {
        loop_ = loop;
    }

    void set_handler(TCPHandler handler) {
        handler_ = std::move(handler);
    }

    atomic<size_t> connections{0};

protected:
    string host_;
    uint16_t port_;
    EventLoop* loop_;
    bool own_loop_{};
    TCPHandler handler_;

    int fd_{};
    bool started_{};
};

class TCPClient {

};

}}
